<?php
/**
 *  增加功能
 */

//~ 判断置顶文章
function span_sticky() {
	if (is_sticky())
		$sticky = ' red';
	else
		$sticky = '';
	return $sticky;
}

//~ 喜欢
function span_like($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '喜欢: ';
	if (isset($_COOKIE['like_' . get_the_ID()])) {
		$like_done = ' done';
		$like_icon = '<i class="icon icon-xihuant xh"></i> ';
	} else {
		$like_done = '';
		$like_icon = '<i class="icon icon-xihuant"></i> ';
	}
	if (get_post_meta(get_the_ID(), 'like', true))
		$like_count = get_post_meta(get_the_ID(), 'like', true);
	else
		$like_count = get_post_like(get_the_ID());
	return '<span class="like"><a data-action="like" data-id="' . get_the_ID() . '" class="postLike' . $like_done . '">' . $like_icon . $text . '<b class="count">' . $like_count . '</b></a></span>';
}

//~ 下载
function span_download($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '下载: ';
	if (get_post_meta(get_the_ID(), 'download', true))
		$download = get_post_meta(get_the_ID(), 'download', true);
	else
		$download = get_post_download(get_the_ID());
	if (get_post_download_text(get_the_ID())) {
		return '<span class="download"><a target="_blank" href="' . get_post_download_url(get_the_ID()) . '" title="' . get_post_download_text(get_the_ID()) . '" data-action="download" data-id="' . get_the_ID() . '" class="postDownload"><i class="icon icon-xiazai"></i> ' . $text . '<b class="count">' . $download . '</b></a></span>';
	}
}

//~ 评论
function span_comment($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '评论: ';
	return '<span class="comment"><a target="_blank" href="' . get_the_permalink() . '#comments"><i class="icon icon-pinglun"></i> ' . $text . get_post(get_the_ID()) -> comment_count . '</a></span>';
}

//~ 分类
function span_cat($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '分类: ';
	$output = '<span class="cat"><i class="icon icon-fenlei"></i> ' . $text;
	foreach (get_the_category() as $category) {
		$value[] = '<a target="_blank" href="' . get_category_link($category -> term_id) . '">' . $category -> cat_name . '</a>';
	}
	$output .= implode(' , ', $value);
	$output .= '</span>';
	return $output;
}

//~ 浏览
function span_views($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '浏览: ';
	return '<span class="views"><i class="icon icon-liulan"></i> ' . $text . get_post_views(get_the_ID()) . '</span>';
}

//~ 标签
function span_tags($text = '') {
	if (empty($text))
		$text = '';
	else
		$text = '标签: ';
	if (get_the_tags()) {
		$output = '<span class="tags es"><i class="icon icon-biaoqian"></i> ' . $text;
		foreach (get_the_tags() as $tag) {
			$value[] = '<a target="_blank" href="' . get_category_link($tag -> term_id) . '">' . $tag -> name . '</a>';
		}
		$output .= implode(' , ', $value);
		$output .= '</span>';
		return $output;
	}
}

//~ 输出文章缩略图
function get_timthumb($width = 100, $height = 80, $share = '') {
	preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_the_content(), $images);
	$src = $images[1][0];
	//~ 如果日志中有图片
	if (!empty($src)) {
		$path = pathinfo($src);
		$name = $path["basename"];
		$url = parse_url($src);
		$host = $url['host'];
		//~ 判断是否是本地图片
		if ($host != $_SERVER['HTTP_HOST']) {
			$file = WP_CONTENT_DIR . '/uploads/cache/' . $name;
			if (!is_file($file) || (time() - filemtime($file)) > 604800) {
				httpcopy($src, $file);
			}
			$src = WP_CONTENT_URL . '/uploads/cache/' . $name;
		}
		if (empty($share)) {
			$img = '<img src="' . get_template_directory_uri() . '/functions/timthumb.php?src=' . $src . '&w=' . $width . '&h=' . $height . '&zc=1" alt="' . $post -> post_title . '" />';
		} else {
			$img = $src;
		}
	} else {
		if (empty($share)) {
			$img = '<img src="' . get_template_directory_uri() . '/functions/timthumb.php?src=' . get_template_directory_uri() . '/images/default.jpg&w=' . $width . '&h=' . $height . '&zc=1"  alt="' . $post -> post_title . '" />';
		} else {
			$img = get_template_directory_uri() . '/images/default.jpg';
		}
	}
	return $img;
}

//~ 时间显示为几分钟之前几小时之前
function timeago($time) {
	$y = get_the_time('Y');
	$m = get_the_time('m');
	$d = get_the_time('d');
	$time = strtotime($time);
	$etime = time() - $time;
	if ($etime < 1)
		return '刚刚';
	$interval = array(12 * 30 * 24 * 60 * 60 => '年前 (<a href="' . get_day_link($y, $m, $d) . '">' . date($m . '-' . $d, $time) . '</a>)', 30 * 24 * 60 * 60 => '个月前 (<a href="' . get_day_link($y, $m, $d) . '">' . date($m . '-' . $d, $time) . '</a>)', 7 * 24 * 60 * 60 => '周前 (<a href="' . get_day_link($y, $m, $d) . '">' . date($m . '-' . $d, $time) . '</a>)', 24 * 60 * 60 => '天前', 60 * 60 => '小时前', 60 => '分钟前', 1 => '秒前');
	foreach ($interval as $secs => $str) {
		$d = $etime / $secs;
		if ($d >= 1) {
			$r = round($d);
			return $r . $str;
		}
	}
}

//~ 评论时间显示为几分钟之前几小时之前
function timecomm($time) {
	$time = strtotime($time);
	$etime = time() - $time;
	if ($etime < 1)
		return '刚刚';
	$interval = array(12 * 30 * 24 * 60 * 60 => '年前 (' . date('m-d', $time) . ')', 30 * 24 * 60 * 60 => '个月前 (' . date('m-d', $time) . ')', 7 * 24 * 60 * 60 => '周前 (' . date('m-d', $time) . ')', 24 * 60 * 60 => '天前', 60 * 60 => '小时前', 60 => '分钟前', 1 => '秒前');
	foreach ($interval as $secs => $str) {
		$d = $etime / $secs;
		if ($d >= 1) {
			$r = round($d);
			return $r . $str;
		}
	}
}

//~ 面包屑导航
function breadcrumbs() {
	$delimiter = '<b>›</b>';
	$home = '首页';
	$showCurrent = 1;
	$before = '<span class="es">';
	$after = '</span>';
	global $post;
	$homeLink = get_bloginfo('url');
	if (is_home() || is_front_page()) {
		$notice = array(new_option('notice_1'), new_option('notice_2'), new_option('notice_3'), new_option('notice_4'), new_option('notice_5'));
		$output = '';
		foreach ($notice as $value)
			if (!empty($value))
				$output .= $value;
		if (!empty($output)) {
			$output = '<div class="notice"><div class="bd"><ul>';
			foreach ($notice as $value)
				if (!empty($value))
					$output .= '<li class="es"><i class="icon icon-wushenglaba"></i>' . $value . '</li>';
			$output .= '</ul></div></div>';
		} else {
			$output .= '<div style="height:15px;"></div>';
		}
		echo $output;
	} else {
		echo '<div class="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
		if (is_category()) {
			$thisCat = get_category(get_query_var('cat'), false);
			if ($thisCat -> parent != 0)
				echo get_category_parents($thisCat -> parent, TRUE, ' ' . $delimiter . ' ');
			echo $before . single_cat_title('', false) . $after;
		} elseif (is_search()) {
			echo $before . '搜索结果："' . get_search_query() . '"' . $after;
		} elseif (is_day()) {
			echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y年') . '</a> ' . $delimiter . ' ';
			echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('m月') . '</a> ' . $delimiter . ' ';
			echo $before . get_the_time('d日') . $after;
		} elseif (is_month()) {
			echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y年') . '</a> ' . $delimiter . ' ';
			echo $before . get_the_time('m月') . $after;
		} elseif (is_year()) {
			echo $before . get_the_time('Y年') . $after;
		} elseif (is_single() && !is_attachment()) {
			if (get_post_type() != 'post') {
				$post_type = get_post_type_object(get_post_type());
				$slug = $post_type -> rewrite;
				echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type -> labels -> singular_name . '</a>';
				if ($showCurrent == 1)
					echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
			} else {
				$cat = get_the_category();
				$cat = $cat[0];
				$cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
				if ($showCurrent == 0)
					$cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
				echo $cats;
				if ($showCurrent == 1)
					echo $before . get_the_title() . $after;
			}
		} elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
			$post_type = get_post_type_object(get_post_type());
			echo $before . $post_type -> labels -> singular_name . $after;
		} elseif (is_attachment()) {
			$parent = get_post($post -> post_parent);
			$cat = get_the_category($parent -> ID);
			$cat = $cat[0];
			echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
			echo '<a href="' . get_permalink($parent) . '">' . $parent -> post_title . '</a>';
			if ($showCurrent == 1)
				echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
		} elseif (is_page() && !$post -> post_parent) {
			if ($showCurrent == 1)
				echo $before . get_the_title() . $after;
		} elseif (is_page() && $post -> post_parent) {
			$parent_id = $post -> post_parent;
			$breadcrumbs = array();
			while ($parent_id) {
				$page = get_page($parent_id);
				$breadcrumbs[] = '<a href="' . get_permalink($page -> ID) . '">' . get_the_title($page -> ID) . '</a>';
				$parent_id = $page -> post_parent;
			}
			$breadcrumbs = array_reverse($breadcrumbs);
			for ($i = 0; $i < count($breadcrumbs); $i++) {
				echo $breadcrumbs[$i];
				if ($i != count($breadcrumbs) - 1)
					echo ' ' . $delimiter . ' ';
			}
			if ($showCurrent == 1)
				echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
		} elseif (is_tag()) {
			echo $before . '标签："' . single_tag_title('', false) . '"' . $after;
		} elseif (is_author()) {
			global $author;
			$userdata = get_userdata($author);
			echo $before . '作者：' . $userdata -> display_name . $after;
		} elseif (is_404()) {
			echo $before . '页面未找到' . $after;
		}
		if (get_query_var('paged')) {
			if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
				echo ' (';
			echo '第' . get_query_var('paged') . '页';
			if (is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author())
				echo ')';
		}
		echo '</div>';

	}
}

//~ 幻灯片
function content_slide($width = 795, $height = 320) {
	query_posts(array('meta_query' => array( array('key' => 'slide')), 'showposts' => 5, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		$output = '<div class="content_slide" style="width:' . $width . 'px;height:' . $height . 'px">';
		$output .= '<div class="bd">';
		$output .= '<ul>';
		$num = 0;
		while (have_posts()) : the_post();
			$output .= '<li style="height:' . $height . 'px">';
			$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb($width, $height) . '</a>';
			$output .= '<h3><a target="_blank" class="es" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a> </h3>';
			$output .= '</li>';
			$num++;
		endwhile;
		$output .= '</ul>';
		$output .= '</div>';
		if ($num > 1) {
			$output .= '<a class="prev"><</a><a class="next">></a>';
			$output .= '<div class="hd">';
			$output .= '<ul>';
			for ($i = 0; $i < $num; $i++)
				$output .= '<li></li>';
			$output .= '</ul>';
			$output .= '</div>';
		}
		$output .= '</div>';
	}
	wp_reset_query();
	echo $output;
}

//~ 文章列表
function content_list() {
	$output = '<div class="content_list">';
	if (is_home())
		$output .= '<div class="title"><h2><i class="icon icon-lianjie"></i> 最新发布</h2></div>';
	$output .= '<ul>';
	if (is_home())
		query_posts(array('orderby' => 'date', 'showposts' => 2, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		while (have_posts()) : the_post();
			$output .= '<li>';
			$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(180, 115) . '</a>';
			$output .= '<div class="info">';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
			$output .= '<div class="subinfo">' . span_cat(text) . span_like(text) . span_download(text) . span_comment(text) . span_views(text) . '</div>';
			$output .= '<p><a target="_blank" href="' . get_the_permalink() . '">' . wp_trim_words(get_the_content(), 70) . '</a></p>';
			$output .= '</div>';
			$output .= '<div class="detail">';
			$output .= '<div class="read"><a target="_blank" href="' . get_the_permalink() . '">阅读全文</a></div>' . span_tags(text);
			$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
			$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
			$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
			$output .= '</div>';
			$output .= '</li>';
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	if (is_home())
		wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;
	if (!is_home())
		the_posts_pagination(array('end_size' => 1, 'mid_size' => 1, 'show_all' => false, 'prev_text' => __('上一页'), 'next_text' => __('下一页')));
}

//~ 分类列表样式一
function content_cat_one($title = '', $number = 4) {
	$cat = new_option('home_category_1', '1');
	$output = '<div class="content_cat content_cat_one">';
	$output .= '<div class="title"><h2><i class="icon icon-lianjie"></i> <a target="_blank" href="' . get_category_link($cat) . '">' . get_cat_name($cat) . '</a></h2></div>';
	$output .= '<ul>';
	query_posts(array('cat' => $cat, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		$first = 1;
		while (have_posts()) : the_post();
			if ($first) {
				$first = $first - 1;
				$output .= '<li class="first">';

				$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(362, 232) . '</a>';
				$output .= '<div class="info">';
				$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
				$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
				$output .= '</div>';
				$output .= '<div class="detail">';
				$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
				$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
				$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
				$output .= '</div>';
				$output .= '</li>';
			} else {
				$output .= '<li class="after">';
				$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(130, 100) . '</a>';
				$output .= '<div class="info">';
				$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
				$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
				$output .= '</div>';
				$output .= '<div class="detail">';
				$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
				$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
				$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
				$output .= '</div>';
				$output .= '</li>';
			}
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;

}

//~ 分类列表样式二
function content_cat_two($title = '', $number = 3) {
	$cat = new_option('home_category_2', '1');
	$output = '<div class="content_cat content_cat_two">';
	$output .= '<div class="title"><h2><i class="icon icon-lianjie"></i> <a target="_blank" href="' . get_category_link($cat) . '">' . get_cat_name($cat) . '</a></h2></div>';
	$output .= '<ul>';
	query_posts(array('cat' => $cat, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		while (have_posts()) : the_post();
			$output .= '<li class="first">';
			$output .= '<a class="img" href="' . get_the_permalink() . '">' . get_timthumb(244.3, 183) . '</a>';
			$output .= '<div class="info">';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
			$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
			$output .= '</div>';
			$output .= '<div class="detail">';
			$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
			$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
			$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
			$output .= '</div>';
			$output .= '</li>';
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;
}

//~ 分类列表样式三
function content_cat_box($title = '', $number = 5) {
	$cat = new_option('home_category_3');
	if (empty($cat))
		$cat = array(1, 2);
	$output = '<div class="content_cat_box">';
	foreach ($cat as $value) {
		$output .= '<div class="content_cat content_cat_three">';
		$output .= '<div class="title"><h2><i class="icon icon-lianjie"></i> <a target="_blank" href="' . get_category_link($value) . '">' . get_cat_name($value) . '</a></h2></div>';
		$output .= '<ul>';
		query_posts(array('cat' => $value, 'showposts' => $number, 'ignore_sticky_posts' => 1));
		if (have_posts()) {
			$first = 1;
			while (have_posts()) : the_post();
				if ($first) {
					$first = $first - 1;
					$output .= '<li class="first">';
					$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(130, 100) . '</a>';
					$output .= '<div class="info">';
					$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
					$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
					$output .= '</div>';
					$output .= '<div class="detail">';
					$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
					$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
					$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
					$output .= '</div>';
					$output .= '</li>';
				} else {
					$output .= '<li class="after">';
					$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '"><b>›</b>' . get_the_title() . '</a></h3>';
					$output .= '<span class="time">' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . '</span>';
					$output .= '</li>';
				}
			endwhile;
		} else {
			$output .= '<li>暂无相关文章</li>';
		}
		wp_reset_query();
		$output .= '</ul>';
		$output .= '</div>';
	}
	$output .= '</div>';
	echo $output;

}

//~ 小工具 标题列表
function sidebar_title($title, $key, $sort, $number) {
	$output = '<div class="sidebar_title">';
	$output .= $title;
	$output .= '<ul>';
	query_posts(array('meta_query' => $key, 'orderby' => $sort, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		$i = 1;
		while (have_posts()) : the_post();
			$output .= '<li>';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '"><b class="num' . $i . '">' . $i . '</b>' . get_the_title() . '</a></h3>';
			$output .= '<span class="time">' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . '</span>';
			$output .= '</li>';
			$i++;
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;
}

//~ 小工具 图文列表
function sidebar_list($title, $key, $sort, $number) {
	$output = '<div class="sidebar_list">';
	$output .= $title;
	$output .= '<ul>';
	query_posts(array('meta_query' => $key, 'orderby' => $sort, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		while (have_posts()) : the_post();
			$output .= '<li>';
			$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(130, 100) . '</a>';
			$output .= '<div class="info">';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
			$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
			$output .= '</div>';
			$output .= '<div class="detail">';
			$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
			$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
			$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
			$output .= '</div>';
			$output .= '</li>';
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;

}

//~ 小工具 最新评论列表
function sidebar_comment($title, $key, $sort, $number) {
	$output = '<div class="sidebar_list">';
	$output .= $title;
	$output .= '<ul>';
	query_posts(array('meta_query' => $key, 'orderby' => $sort, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		while (have_posts()) : the_post();
			$output .= '<li>';
			$output .= '<a style="height:124px" class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(130, 124) . '</a>';
			$output .= '<div class="info">';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
			$output .= '</div>';
			$comments = get_comments(array('number' => 2, 'post_id' => get_the_ID(), 'status' => 'approve'));
			foreach ($comments as $comment) {
				$output .= '<div class="comm">';
				$output .= '<span class="avatar">' . get_avatar($comment -> comment_author_email, '38') . '</span>';
				$output .= '<b><span class="author es"><a target="_blank" href="' . get_permalink($post -> comment_post_ID) . '#li-comment-' . $comment -> comment_ID . '">' . $comment -> comment_author . '</a></span>';
				$output .= '<span class="time">' . timecomm($comment -> comment_date_gmt) . '</span></b>';
				$output .= '<span class="retext es"><a target="_blank" href="' . get_permalink($post -> comment_post_ID) . '#li-comment-' . $comment -> comment_ID . '">' . $comment -> comment_content . '</a></span>';
				$output .= '</div>';
			}
			$output .= '</li>';
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;

}

//~ 小工具 分类列表
function sidebar_cat($title, $cat, $number) {
	$output = '<div class="sidebar_list">';
	$output .= $title;
	$output .= '<ul>';
	query_posts(array('cat' => $cat, 'showposts' => $number, 'ignore_sticky_posts' => 1));
	if (have_posts()) {
		while (have_posts()) : the_post();
			$output .= '<li>';
			$output .= '<a class="img" target="_blank" href="' . get_the_permalink() . '">' . get_timthumb(130, 100) . '</a>';
			$output .= '<div class="info">';
			$output .= '<h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '">' . get_the_title() . '</a></h3>';
			$output .= '<div class="subinfo">' . span_like() . span_download() . span_comment() . span_views() . '</div>';
			$output .= '</div>';
			$output .= '<div class="detail">';
			$output .= '<span class="avatar">' . get_avatar(get_the_author_meta('ID'), '38') . '</span>';
			$output .= '<span class="author es">' . get_the_author_posts_link() . '</span>';
			$output .= '<span class="time es"> ' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . ' </span>';
			$output .= '</div>';
			$output .= '</li>';
		endwhile;
	} else {
		$output .= '<li>暂无相关文章</li>';
	}
	wp_reset_query();
	$output .= '</ul>';
	$output .= '</div>';
	echo $output;

}

//~ 百度分享
function get_share() {
	$output = '<div class="bdsharebuttonbox">';
	$output .= '<span>分享给小伙伴：</span>';
	$output .= '<a class="bds_weixin" data-cmd="weixin"></a>';
	$output .= '<a class="bds_sqq" data-cmd="sqq"></a>';
	$output .= '<a class="bds_qzone" data-cmd="qzone"></a>';
	$output .= '<a class="bds_tsina" data-cmd="tsina"></a>';
	$output .= '</div>';
	$output .= '<script>';
	$output .= 'window._bd_share_config = {';
	$output .= 'common : {';
	$output .= 'bdText : "' . get_the_title() . ' | ' . get_bloginfo('name') . ' - ' . get_bloginfo('description') . '",';
	$output .= 'bdUrl : "' . get_permalink() . '",';
	$output .= 'bdPic : "' . get_timthumb(100, 100, share) . '"';
	$output .= '},';
	$output .= 'share : [{';
	$output .= '"bdCustomStyle" : "' . get_template_directory_uri() . '/style/style.css"';
	$output .= '}]';
	$output .= '}';
	$output .= '</script>';
	echo $output;
}

//~ 上一篇文章，下一篇文章导航
function single_page() {
	if (!is_page()) {
		echo '<div class="single_page"><span class="es">';
		if (get_previous_post())
			previous_post_link('上一篇：%link');
		else
			echo "上一篇：没有了，已经是最后一篇文章";
		echo '</span><span class="es">';
		if (get_next_post())
			next_post_link('下一篇：%link');
		else
			echo "下一篇：没有了，已经是最新文章";
		echo '</span></div>';
	}
}

//~ 相关文章
function single_related() {
	if (!is_page()) {
		$output = '<div class="single_related">';
		$output .= '<div class="title"><h2><i class="icon icon-fenlei"></i> 相关文章</h2></div>';
		$output .= '<ul>';
		if (wp_get_post_tags(get_the_ID())) {
			//~ 获取标签列表
			foreach (wp_get_post_tags(get_the_ID()) as $tag)
				$tag_list[] .= $tag -> term_id;
			query_posts(array('tag__in' => array($tag_list[mt_rand(0, count($tag_list) - 1)]), 'category__not_in' => array(NULL), 'post__not_in' => array(get_the_ID()), 'showposts' => 2, 'caller_get_posts' => 1));
			while (have_posts()) : the_post();
				$output .= '<li><h3><a target="_blank" class="es' . span_sticky() . '" href="' . get_the_permalink() . '" title="' . get_the_title() . '"><b>›</b>' . get_the_title() . '</a></h3>';
				$output .= '<span class="time">' . timeago(get_gmt_from_date(get_the_time('Y-m-d G:i:s'))) . '</span></li>';
			endwhile;
			wp_reset_query();
		} else {
			$output .= '<li>暂无相关文章</li>';
		}
		$output .= '</ul>';
		$output .= '</div>';
		echo $output;
	}
}
?>
